package com.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @Package com.util
 * @Date 2024-05-23 12:24
 * 密码加密功能，用不用得到再说反正我写了
 */

public class PasswordUtil {

    // 密码加密
    public static String encryptPassword(String password) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            byte[] hashedBytes = md.digest(password.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte b : hashedBytes) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("No such algorithm exception", e);
        }
    }

    // 验证密码
    public static boolean verifyPassword(String inputPassword, String storedHash) {
        return encryptPassword(inputPassword).equals(storedHash);
    }
}